{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5626dae8-008d-441e-bd76-a799a4bb15ba",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# 标准欧氏距离\n",
    "Book_6《数据有道》 | 鸢尾花书：从加减乘除到机器学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c42a0dbd-3aec-4d40-8445-20f1540959d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.spatial import distance\n",
    "import numpy as np\n",
    "\n",
    "# Variance-covariance matrix\n",
    "SIGMA = np.array([[2, 1], [1, 2]])\n",
    "\n",
    "q   = [0, 0];       # query point\n",
    "x_1 = [-3.5, -4];   # data point 1\n",
    "x_2 = [2.75, -1.5]; # data point 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb5fef77-9256-4878-813c-4bdb9bf888c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate standardized Euclidean distances\n",
    "\n",
    "d_1 = distance.seuclidean(q, x_1, np.diag(SIGMA))\n",
    "d_2 = distance.seuclidean(q, x_2, np.diag(SIGMA))\n",
    "\n",
    "# Note1: V is an 1-D array of component variances"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
